class Admin::OptionsController < Admin::BaseController
  
  def index
    list
    render :action => 'list'
  end  
  
  def new
    @option = Option.new
  end
  
  def list
    @options =  Option.find(:all, :page => {:size => 10, :current =>params[:page], :first => 1})
  end
  
  def create
    @option = Option.new(@params['option'])
    @option.option_values = OptionValue.find(params[:option_values]) if params[:option_values]
    @option.save!
    flash[:notice] = 'Option was successfully created.'
    redirect_to :action => 'index'    
  rescue
    logger.error("unable to create new option: #{@option.inspect}")
    flash[:notice] = 'ERROR - Problem saving new option'
    # error occurred while saving, give user a chance to save again
    render :action => 'new'
  end    
  
  def edit
    @option = Option.find(@params[:id])
  end
  
  def show
    @option = Option.find(@params[:id])
  end
  
  def update
    @option = Option.find(@params[:id])
    if @option.update_attributes(@params[:option])
      flash[:notice] = 'Option was successfully updated.'
      redirect_to :action => 'index', :id => @option
    else
      render :action => 'edit'
    end
  end
  
  def destroy
    Option.find(params[:id]).destroy
    redirect_to :action => 'index'
  end
  
end